package com.zrx.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrx.springboot.entity.User;
import com.zrx.springboot.mapper.UserMapper;
import com.zrx.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 检验用户名的存在性
     *
     * @param username
     * @return
     */
    @Override
    public String checkUser(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return "101";
            //用户名不存在
        } else {
            return "102";
            //用户名已存在
        }
    }

    /**
     * 登入验证
     *
     * @param
     * @return
     */
    @Override
    public String loginCheck(User loginUser, HttpSession session, String vehicle, HttpServletResponse response, HttpServletRequest request) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", loginUser.getUsername());
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return "101";
            //用户名不存在
        } else {
            if (loginUser.getPassword().equals(user.getPassword())) {
                session.setAttribute("user", user);
                //勾选-记住密码(写Cookie)
                if (vehicle !=null) {
                    Cookie cookieusername = new Cookie("username", user.getUsername());
                    Cookie cookiepassword = new Cookie("password", user.getPassword());
                    System.out.println("------------");
                    System.out.println(user.getUsername()+"----"+user.getPassword());
                    System.out.println("------------");
                    response.addCookie(cookieusername);
                    response.addCookie(cookiepassword);
                    cookieusername.setMaxAge(60 * 60*24);
                    cookiepassword.setMaxAge(60 * 60*24);
                    cookieusername.setPath(request.getContextPath());
                    cookiepassword.setPath(request.getContextPath());
                } else {//未勾选-记住密码(删除Cookie)
                    Cookie cookieusername = new Cookie("username", "");
                    Cookie cookiepassword = new Cookie("password", "");
                    // 设置持久时间为0
                    cookieusername.setMaxAge(0);
                    cookiepassword.setMaxAge(0);
                    // 设置共享路径
                    cookieusername.setPath(request.getContextPath());
                    cookiepassword.setPath(request.getContextPath());
                    // 向客户端发送 Cookie
                    response.addCookie(cookieusername);
                    response.addCookie(cookiepassword);
                }

                return "100";
                //密码正确
            } else {
                return "102";
                //密码不正确
                // }
            }

        }
    }
}

